package com.lj.demo.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.demo.commonutils.R;
import com.lj.demo.eduservice.entity.CourseInfoForm;
import com.lj.demo.eduservice.entity.CoursePublishVo;
import com.lj.demo.eduservice.entity.CourseQuery;
import com.lj.demo.eduservice.entity.EduCourse;
import com.lj.demo.eduservice.service.EduChapterService;
import com.lj.demo.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author liujiang
 * @since 2020-12-03
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/eduCourse")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @ApiOperation(value = "根据课程id删除课程信息")
    @DeleteMapping("deleteByCourseId/{courseId}")
    public R deleteByCourseId(@PathVariable String courseId){
        eduCourseService.deleteCourseQueryByCourseId(courseId);
        return R.ok();
    }


    @ApiOperation(value = "分页查询课程列表")
    @PostMapping("getCourseListPage/{page}/{limit}")
    public R getCourseListPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) CourseQuery courseQuery){
        Page<EduCourse> coursePage = new Page<>(page,limit);
        eduCourseService.pageQuery(coursePage,courseQuery);
        long total = coursePage.getTotal(); //得到的总记录数
        List<EduCourse> records = coursePage.getRecords();//得到的课程数据
        return R.ok().data("total",total).data("rows",records);
    }


    /***********************************************************/



    @ApiOperation(value = "添加课程基本信息")
    @PostMapping("addCourse")
    public R addCourse(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm){
        String id = eduCourseService.addCourse(courseInfoForm);
        return R.ok().data("courseId",id);
    }

    @ApiOperation(value = "根据id查询课程基本信息")
    @GetMapping("getById/{id}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
            CourseInfoForm courseInfoForm = eduCourseService.getAllById(id);
        return R.ok().data("courseInfoForm",courseInfoForm);
    }

    @ApiOperation(value = "更新课程基本信息")
    @PostMapping("updateCourse/{id}")
    public R updateCourse(
            @RequestBody CourseInfoForm courseInfoForm,
            @PathVariable String id){
        String cid =eduCourseService.updateCouAndDesc(courseInfoForm);
        return R.ok().data("courseId",cid);
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getCoursePublishVo/{id}")
    public R getCoursePublishVo(@PathVariable String id){
       CoursePublishVo coursePublishVo = eduCourseService.getCoursePubishById(id);
        return R.ok().data("items",coursePublishVo);
    }

    @ApiOperation(value = "根据id发布课程")
    @PostMapping("publishCourseById/{id}")
    public R publishCourseById(@PathVariable String id){
        eduCourseService.publishCourse(id);
        return R.ok();
    }



}

